package client.comm.baoding.ui

import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import client.comm.commlib.comm_ui.draw.SpacePblGridItemDecoration
import client.comm.commlib.widget.EmptyLayout
import client.comm.baoding.R
import client.comm.baoding.adapter.LookMoreAdapter
import client.comm.baoding.databinding.ActivityLookmoreBinding
import client.comm.baoding.ui.vm.LookMoreModel
import client.comm.baoding.BaseMiddleActivity
class LookMoreActivity : BaseMiddleActivity<LookMoreModel, ActivityLookmoreBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_lookmore
    }

    val goodsAdapter: LookMoreAdapter by lazy {
        LookMoreAdapter(this, mImgLoader)
    }

    override fun initView() {
        binding.recyclerView.adapter = goodsAdapter
        val gridLayoutManager =
            StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        binding.recyclerView.layoutManager = gridLayoutManager
        binding.recyclerView.addItemDecoration(
            SpacePblGridItemDecoration(
                client.comm.commlib.dipToPx(resources!!, 10.0f).toInt(),
                binding.recyclerView
            )
        )
        binding.emptyLayout.setOnClickListener {
            viewModel.pageOne.value = 0
            viewModel.holdOne()
        }
        binding.recyclerView.onLoadMore = {
            viewModel.holdOne()
        }
        binding.refreshLayout.setOnRefreshListener {
            viewModel.pageOne.value = 0
            viewModel.holdOne()
        }
        intent.getStringExtra("word")?.let {
            viewModel.wordVal.value = it
        }
        binding.event = this
        binding.emptyLayout.visibility = View.GONE
    }

    override fun observerUI() {
        viewModel.callOne.observe(this, Observer {
            it?.let {
                if (it.isSuccess) {
//                    val list =
//                        it.data?.record_list as MutableList<Zcjl.Record?>?
//                    if (viewModel.pageOne.value == 1) {
//                        goodsAdapter.submitList(list)
//                    } else
//                        goodsAdapter.appendList(list)
//                    if (list != null && list.size > 0) {
//                        if (list.size < 10) {
//                            goodsAdapter.hasMoreState.value = false
//                        }
//                        binding.emptyLayout.visibility = View.GONE
//                    } else {
//                        if (goodsAdapter.itemCount == 0)
//                            binding.emptyLayout.errorType = EmptyLayout.NODATA
//                        goodsAdapter.hasMoreState.value = false
//                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
    }

    override fun initViewModel(): LookMoreModel = createViewModel()

}